package com.dragon.mmochat.message;

import java.text.DecimalFormat;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.bet.MmochatBet;
import com.dragon.mmochat.model.bet.MmochatBetInfo;
import com.dragon.mmochat.model.bet.MmochatBetRate;
import com.dragon.mmochat.model.enumType.MmochatBetType;
import com.dragon.mmochat.model.role.MmochatPlayer;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

//下发赌场基本数据(赌场类型，当前压注状态，金钱数/元宝数，赔率，玩家压注数，我的压注数)
public class Mmochat_17_5S extends SkymobiProtocolMessage {
	MmochatBet bet;
	MmochatPlayer me;
	boolean openWin = false;
	String systemMsg = "";

	public Mmochat_17_5S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_17_5);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (openWin) {
			out.put((byte) 1);
		} else {
			out.put((byte) 0);
		}
		// 赌场类型
		out.put((byte) bet.getBetType().getKey());
		// 当前压注状态
		out.put((byte) bet.getBetRoomState().getKey());
		// 我的金钱数/元宝数
		if (bet.getBetType() == MmochatBetType.游戏币赌场) {
			out.putInt((int) me.getSmall_money());
		} else if (bet.getBetType() == MmochatBetType.元宝赌场) {
			out.putInt((int) me.getBig_money());
		}
		out.put((byte) bet.getBetNum());
		DecimalFormat df = new DecimalFormat("0.0");
		DecimalFormat df2 = new DecimalFormat("0");
		MmochatBetInfo betInfo = bet.getBetMap().get(me.getRoleId());
		for (int i = 0; i < bet.getBetNum(); i++) {
			// 赔率
			MmochatBetRate betRate = bet.getBetRates()[i];
			out.putPrefixedString(df.format(betRate.getAward()), 1);
			// 玩家总压注
			out.putPrefixedString(df2.format(bet.getTotalBetMoney(i)), 1);
			// 我的压注数
			if (betInfo != null) {
				Integer money = betInfo.getBetMoneyMap().get(i);
				if (money == null) {
					out.putInt(0);
				} else {
					out.putInt(money);
				}
			} else {
				out.putInt((byte) 0);
			}
		}
		out.putPrefixedString(systemMsg, 2);
	}

	public void setBet(MmochatBet bet) {
		this.bet = bet;
	}

	public void setMe(MmochatPlayer me) {
		this.me = me;
	}

	public void setOpenWin(boolean openWin) {
		this.openWin = openWin;
	}

	public void setSystemMsg(String systemMsg) {
		this.systemMsg = systemMsg;
	}

}
